
/**   
  * Table
  *
  * author Tino
 */
(function ($) {
	$.fn.extend({
		addRow : function (options) {
			var rowIndex = this.find('tr').length;
			
			options = jQuery.extend({}, options);
			
			var tr = $("<tr></tr>");
			tr.addClass(options.class||'');
			
			if(rowIndex%2==0)
			tr.addClass('bgfafd');
			
			tr.append('<td>'+rowIndex+'</td>');
			
			var _tdArr = options.td||[];
			for(var i = 0;i<_tdArr.length;i++){
				var _td = _tdArr[i];
				
				var td = $('<td></td>');
				td.html(_td.text || '');
				td.attr('data',_td.text);
				td.addClass(_td.class || '');
				
				var _tdEditable = _td.editable ? true : false;
				if(_tdEditable)td.addClass('editable');
				
				var _tdHidden = _td.hidden ? true : false;
				if(_tdHidden)td.css('display','none');
				
				tr.append(td);
			}
			this.append(tr);
			
			this.charge();

			this.updateTable();
			
		},
		updateTable : function(){
			var rows = this.find('tr');
			var totalMoney = '0.00',totalGoods = '0',totalPay = '0.00',totalDiscount = '0.00',Point = '0',beforeTotalPoint = $('#point').val();
			
			for(var i = 1;i<rows.length;i++){
				var _tr = rows.eq(i);
				var _tdArr = _tr.find('td');
				var _td = _tdArr.eq(0);
				_td.html(i);
				
				var totalMoneyText = _tdArr.eq(8).html().substring(1,_tdArr.eq(8).html().length);//吊牌价
//				var discount = jQuery.multiply((100 - Number(_tdArr.eq(6).html())), 0.01);//折扣
				var num = Number(_tdArr.eq(11).html());//数量
				var totalPayText = _tdArr.eq(12).html().substring(1,_tdArr.eq(12).html().length);//金额
				totalGoods = jQuery.sum(Number(totalGoods), num);
				totalPay = jQuery.sum(Number(totalPay), Number(totalPayText));
				var money = jQuery.multiply(Number(totalMoneyText), num);
				totalMoney = jQuery.sum(Number(totalMoney), Number(money));
				var index = totalPay.indexOf('.');
				if(index!=-1){
					Point = totalPay.substring(0,index);
				}else{
					Point = totalPay;
				}
				
				Point = Math.floor(Point/moneyPointProp);
				//alert(totalGoods+'/'+totalPay+'/'+totalMoney+'/'+totalDiscount+'/'+Point+'/'+beforeTotalPoint);
			}
			
			//alert(totalGoods+'/'+totalPay+'/'+totalMoney+'/'+totalDiscount+'/'+Point+'/'+beforeTotalPoint);
			$('#totalGoodsSpan').html(totalGoods);
			$('#totalPaySpan').html(totalPay);
			$('#totalMoneySpan').html(totalMoney);
			$('#totalDiscountSpan').html(parseFloat(totalMoney-totalPay).toFixed(2));
			$('#PointSpan').html(Point);
			$('#totalPointSpan').html(jQuery.sum(Number(Point), Number(beforeTotalPoint)));
			$('#shouldPay').val(totalPay);

		},
		charge : function(){
			var charge = Number('0.00');
			var shouldPay = Number($('#shouldPay').val());
			var wipeZero = $('#wipeZero').val().trim().length<1&&((Number($('#wipeZero').val().trim())+"")=="NaN")?Number(0):Number($('#wipeZero').val().trim());
			var realPay = $('#realPay').val().trim().length<1&&((Number($('#realPay').val().trim())+"")=="NaN")?Number(0):Number($('#realPay').val().trim());
			var consumePoint = Number($('#consumePoint').val());
//			alert("aa"+shouldPay);
			var pointMoney = consumePoint/pointMoneyProp;
			pointMoney = jQuery.multiply(pointMoney,1);
			shouldPay = jQuery.reduce(shouldPay,pointMoney);
			shouldPay = jQuery.reduce(shouldPay,wipeZero);//此时shouldpay的值表示去掉积分抵现和抹零后客户真正要支付的钱
//			alert("去掉折扣抵现和抹零之后的shouldpay="+shouldPay+"抵现="+pointMoney+"wipeZero="+wipeZero+"实付="+realPay);
			if(shouldPay<=realPay){
				if(shouldPay<=0){
					charge = realPay;
				}else{
					charge = jQuery.reduce(realPay,shouldPay);
				}
			}else{
				charge = jQuery.reduce(realPay,shouldPay);
				if(charge>realPay)
				charge = realPay;
			}
			
			$('#charge').val(charge);
			$('#PointSpan').html(Math.floor(($('#realPay').val()-$('#charge').val())/moneyPointProp));
		},
		grandTable : function(){
			var items = [];
			
			var rows = this.find('tr');
			for(var i = 1;i<rows.length;i++){
				var _tr = rows.eq(i);
				var _tdArr = _tr.find('td');
				
				var _td = _tdArr.eq(1);//商品编号
				items.push(_td.html());
				
				_td = _tdArr.eq(10);//单价
				var data = _td.html();
				items.push(data);
				
				_td = _tdArr.eq(11);//数量
				items.push(_td.html());
				_td = _tdArr.eq(12);//金额
				items.push(_td.html().substring(1,_td.html().length));
			}
			
			$('#Items').val(items.join(','));
			$('#Total').val($('#shouldPay').val());
			$('#RealPay').val($('#realPay').val());
			$('#Charge').val($('#charge').val());
			$('#PointNum').val($('#consumePoint').val());
			$('#WipeZero').val($('#wipeZero').val());
		}
	});
})(jQuery);
